题目
Although Lemmings can walk, fall, and dig, Lemmings aren’t invulnerable. If a Lemming falls for too long then hits the ground, it can splatter. In particular, if a Lemming falls for more than 20 clock cycles then hits the ground, it will splatter and cease walking, falling, or digging (all 4 outputs become 0), forever (Or until the FSM gets reset). There is no upper limit on how far a Lemming can fall before hitting the ground. Lemmings only splatter when hitting the ground; they do not splatter in mid-air. ![在这里插入图片描述](https://img-blog.csdnimg.cn/ff6f335eba654e3f9e7abd7bcb1dfdde.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAZmFueXVhbmRyag==,size_20,color_FFFFFF,t_70,g_se,x_16)
解题思路
与Lemmings3相比Lemmings4需要多加一个状态以及一个计数器。当left_falll或者right_fall的持续时间超过20个时钟周期时就进入die状态,直到系统复位。
代码如下
module top_module(
input clk,
input areset, // Freshly brainwashed Lemmings walk left.
input bump_left,
input bump_right,
input ground,
input dig,
output walk_left,
output walk_right,
output aaah,
output digging );
parameter left = 0;
parameter right = 1;
parameter left_dig = 2;
parameter right_dig = 3;
parameter left_fall = 4;
parameter right_fall = 5;
parameter die = 6;
reg[2:0] state, next_state;
reg[3:0] out;
reg [4:0] cnt;
reg flag;
//fall状态计数器
always@(posedge clk or posedge areset) begin
if(areset)
cnt |